package com.dream.controller;

import com.alibaba.fastjson.JSON;
import com.dream.pojo.Clazz;
import com.dream.pojo.Department;
import com.dream.pojo.Major;
import com.dream.service.ClazzService;
import com.dream.service.DepartmentService;
import com.dream.service.MajorService;
import com.dream.service.impl.ClazzServiceImpl;
import com.dream.service.impl.DepartmentServiceImpl;
import com.dream.service.impl.MajorServiceImpl;
import com.dream.utils.PageUtil;
import com.dream.utils.ResultCommon;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/MajorController.do")
public class MajorController extends BaseServlet{
    MajorService majorService = new MajorServiceImpl();

    DepartmentService departmentService = new DepartmentServiceImpl();

    ClazzService clazzService = new ClazzServiceImpl();

    /**
     * 分页查询
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String majorName = request.getParameter("majorName"); //条件1
        String pageIndex = request.getParameter("pageIndex"); //条件4  当前页码
        if (pageIndex == null || "".equals(pageIndex)) {
            pageIndex = "1";//默认值
        }
        String pageSize = request.getParameter("pageSize"); //条件5   页面大小
        if (pageSize == null || "".equals(pageSize)) {
            pageSize = "5";//默认值
        }
        //条数
        long totalCount = majorService.totalCount(majorName);
        //每页数据
        List<Major> departmentList = majorService.list(majorName, Long.valueOf(pageIndex), Long.valueOf(pageSize));
        for (Major major : departmentList) {
            Department department = departmentService.findById(major.getDeptId());
            major.setDeptName(department.getDeptName());
        }
        //封装一个分页的工具类
        PageUtil<Major> pageUtil = new PageUtil<Major>(Long.valueOf(pageIndex), Long.valueOf(pageSize), totalCount, departmentList);
        //存值转发
        request.setAttribute("pageUtil", pageUtil);
        request.setAttribute("majorName", majorName); //回显
        request.getRequestDispatcher("viewMajorInfo.jsp").forward(request, response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultCommon resultCommon = null;
        String majorId = request.getParameter("majorId");
        String majorName = request.getParameter("majorName");
        String deptId = request.getParameter("deptId");
        String remark = request.getParameter("remark");
        Major major = new Major(majorId, deptId, majorName, remark);
        int count = majorService.add(major);
        if (count > 0) {
            resultCommon = new ResultCommon(200, "新增成功", null);
        } else {
            resultCommon = new ResultCommon(500, "新增异常", null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ResultCommon resultCommon=null;
        String majorId = request.getParameter("majorId");
        String majorName = request.getParameter("majorName");
        String deptId = request.getParameter("deptId");
        String remark = request.getParameter("remark");
        Major major = new Major(majorId, deptId, majorName, remark);
        int count = majorService.update(major);
        if (count > 0) {
            resultCommon=new ResultCommon(200,"更新成功",null);
        } else {
            resultCommon=new ResultCommon(500,"更新异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }

    public void goupdate(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
      String majorId=request.getParameter("majorId");
      String deptId=majorService.findById(majorId).getDeptId();
      request.setAttribute("deptId",deptId);
      Major major = majorService.findById(majorId);
      request.setAttribute("major",major);
      request.getRequestDispatcher("updateMajor.jsp").forward(request,response);
    }

    public void delete(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String majorId = request.getParameter("majorId");
        //判断这个专业下是否有班级
        List<Clazz> clazzList = clazzService.findClazzByMajorId(majorId);
        if (clazzList.size()>0){
            response.getWriter().write("<script>alert('专业在使用中，勿删除！');location.href='/MajorController.do?action=list';</script>");
        }else {
            int count = majorService.deleteById(majorId);
            if (count>0){
                response.getWriter().write("<script>alert('专业删除成功！');location.href='/MajorController.do?action=list';</script>");
            }else {
                response.getWriter().write("<script>alert('专业删除失败！');location.href='/MajorController.do?action=list';</script>");
            }
        }
    }
    //根据院系ID查询专业信息
    public void getMajorByDeptId(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String deptId = request.getParameter("deptId");
        ResultCommon resultCommon=null;
        List<Major> majorList= majorService.findMajorsByDepartId(deptId);
        if (majorList.size() > 0) {
            resultCommon=new ResultCommon(200,"查询成功",majorList);
        } else {
            resultCommon=new ResultCommon(500,"查询异常",null);
        }
        String toJSONString = JSON.toJSONString(resultCommon);
        response.getWriter().write(toJSONString);
    }
}
